<!DOCTYPE html>
<html>
  <head>
    <title>
      audit.js: handling failures
    </title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      // The task runner will print out a console message to ensure the text
      // file comparison.
      let audit = Audit.createTaskRunner({requireResultFile: true});


      // These assertions are supposed to be failed. The WPT (testharness) fails
      // because the assertions are failing, but the existence of expected test
      // result makes this test pass as long as the actual and the expected
      // match. See the expected result file to see the logging message from
      // Audit.


      // Basic assertions.
      audit.define(
          {
            label: 'basic-failure',
            description: 'Testing basic assertion failures.'
          },
          function(task, should) {
            should(function() {
              let foo = bar;
            }, 'Setting foo to bar').notThrow();
            should(function() {
              let foo = 0;
            }).throw(ReferenceError);
            should(3 > 5, '3 < 5').beTrue();
            should(true).beFalse();
            should(1, 'The number of channels').beEqualTo(2);
            should(1).notBeEqualTo(1);
            should(1).beNaN();
            should(NaN).notBeNaN();
            should(typeof 3.141592, 'typeof 3.141592').beEqualTo('string');
            should(1).beGreaterThan(2);
            should(1).beGreaterThanOrEqualTo(2);
            should(2).beLessThan(1);
            should(2).beLessThanOrEqualTo(1);
            should(should(1).beEqualTo(2), 'should(1).beEqualTo(2)').beTrue();
            should(false, 'The message is').message('truthful!', 'false!');

            let oac = new OfflineAudioContext(1, 128, 44100);
            Promise
                .all([
                  should(
                      oac.decodeAudioData(),
                      'Decoding audio data with no argument')
                      .beResolved(),
                  should(oac.startRendering(), 'Start OAC rendering')
                      .beRejected(),
                  should(oac.suspend(), 'Suspending OAC with no argument')
                      .beRejectedWith('IndexSizeError')
                ])
                .then(task.done.bind(task));
          });


      // Numerical assertions.
      audit.define(
          {
            label: 'numerical-failures',
            description: 'Testing numerical assertion failures.'
          },
          function(task, should) {
            should(0).beCloseTo(0.1, {threshold: 0});
            should(59, 'The measured decibel').beCloseTo(62, {threshold: 0.01});
            should([1, 8, 11, 18, 6, 5, 5, 5, 123, 49], 'Test array')
                .beConstantValueOf(5);
            should([0, 0, 0]).notBeConstantValueOf(0);
            should([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).beEqualToArray([
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            ]);
            should([1, 1, 1, 1, 2, 2, 3, 3, 3]).containValues([1, 3, 2]);
            should([1,1,2,2]).containValues([1,2,3]);
            should([1,2,3,4,5]).containValues([1,2,3]);
            should([0.5, 0.5, 0.55, 0.5, 0.45, 0.5]).notGlitch(0.04);
            should([
              0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9
            ]).beCloseToArray([0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1], {
              absoluteThreshold: 0.02
            });
            should([
              0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9
            ]).beCloseToArray([0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1], {
              relativeThreshold: 0.1
            });
            should([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.9])
                .beCloseToArray(
                    [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1],
                    {absoluteThreshold: 0.02, relativeThreshold: 0.1});

            // Tests extra info
            should([
              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
            ]).beCloseToArray([2, 3, 4, 5, 6, 7, 800, 900, 1000, 1100, 1200], {
              absoluteThreshold: 0
            });
            task.done();
          });

      // Testing the failure handling of Audit.loadFileFromUrl().
      audit.define(
          {
            label: 'load-file-in-should',
            description:
                'Testing the failure handling of Audit.loadFileFromUrl().'
          },
          (task, should) => {
            let url = 'non-existent-audio-file.wav';
            let promise =
                should(
                    Audit.loadFileFromUrl(url),
                    'Loading non-existent file within should().beResolved()')
                    .beResolved();
            promise.then(() => {task.done()});
          });

      // With no argument, this executes all tasks in the order defined.
      audit.run();
    </script>
  </body>
</html>
